package com.alisls.shequtao.service.user.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JdbcTokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;

import javax.annotation.Resource;
import javax.sql.DataSource;

/**
 * Token配置类
 * 用于配置Token的管理策略
 *
 * @author Ke Wang
 */
@Configuration
@RefreshScope
public class TokenConfig {

    /**
     * JwtToken对称加密密钥
     */
    @Value("${oauth.jwtToken.symmetric.signingKey}")
    private String signingKey;

    /**
     * jwtTokenStore转换器
     * @return jwtAccessTokenConverter转换器实例
     */
    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter() {
        JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
        // 设置对称加密密钥，对应的资源服务器也要使用此密钥来解密
        jwtAccessTokenConverter.setSigningKey(signingKey);
        return jwtAccessTokenConverter;
    }

    /**
     * 令牌管理策略
     * 向容器注入 jwtTokenStore
     * @return jwtTokenStore示例
     */
    @Bean
    public TokenStore jwtTokenStore() {
        return new JwtTokenStore(jwtAccessTokenConverter());
    }

}
